import { CommonService } from "~/utils/common.service";

export default {
  departmentTreeData(state) {
    return (deptType: string) => {
      const departmentList: any[] = state.departmentList.filter(x => {
        return deptType ? x.deptType === deptType : true
      }).map(v => ({ ...v }))
      return CommonService.generateTreeData(departmentList, {
        keyName: 'id',
        parentKeyName: 'parentId'
      })
    }
  },

  departmentTreeDataTwo(state) {
    return (deptType1: string,deptType2: string) => {
      const departmentList: any[] = state.departmentList.filter(x => {
        return deptType1 && deptType2 ? (x.deptType === deptType1 || x.deptType === deptType2) : true
      }).map(v => ({ ...v }))
      return CommonService.generateTreeData(departmentList, {
        keyName: 'id',
        parentKeyName: 'parentId'
      })
    }
  },

  /**
   * 用户部门数据
   * @param param0
   */
  departmentData(state, getters) {
    return getters.departmentTreeData()
  },
  /**
   * 用户部门数据
   * @param param0
   */
  visitDepartmentData(state, getters) {
    return getters.departmentTreeData("VISIT")
  },

  /**
   * 电话催收部门数据
   * @param param0
   */
  callDepartmentData(state, getters) {
    return getters.departmentTreeData("TEL")
  },

  /**
   * 外访部门数据无root
   * @param param0
   */
  visitDepartmentDataNoRoot(state, getters) {
    return getters.departmentTreeData("VISIT")
  },

  /**
   * 法务催收部门数据
   * @param param0
   */
  legalDepartmentData(state, getters) {
    return getters.departmentTreeData("LEGAL")
  },

  /**
   * 返回是否超级管理员
   */
  isSupperAdmin(state, getters) {
    return state.userData.companyCode === null;
  },

  /**
   * 欺诈部门数据无root
   * @param param0
   */
  cheatDepartmentDataNoRoot(state, getters) {
    return getters.departmentTreeData("CHEAT")
  },

  /**
   * 诉讼部门数据无root
   * @param param0
   */
  lawDepartmentDataNoRoot(state, getters) {
    return getters.departmentTreeData("LAW")
  },
  /**
   * 外诉部门数据无root
   * @param param0
   */
  OutLawDepartmentDataNoRoot(state, getters) {
    return getters.departmentTreeData("OUT_LAW")
  },
  /**
   * 信用修复部门数据 无root
   * @param state 
   * @param getters 
   */
  creditRepairDepartmentDataNoRoot(state, getters) {
    return getters.departmentTreeData("REPAIR")
  },
  getAllParentId(state) {
    const findParent = (id: string, type: string): string[] => {
      const departmentList: any[] = state.departmentList.filter(x => {
        return type ? x.deptType === type : true
      })
      if (!departmentList.length) return [id]
      const current = departmentList.find(x => x.id === id)
      if (!current) return [id]
      const parent = departmentList.find(x => x.id === current.parentId)
      if (parent) {
        return findParent(parent.id, type).concat(current.id)
      } else {
        return [current.id]
      }
    }
    return findParent
  },
  /**
   * 根据ID获取deptCode
   * @param state 
   */
  getDeptCodeById(state) {
    return (id: string) => {
      const dept = state.departmentList.find(x => x.id === id)
      return dept ? dept.deptCode : ''
    }
  },
  /**
   * 获取电催和法务的数据
   * @param param0
   */
  departmentDataTwo(state, getters) {
    return getters.departmentTreeDataTwo("TEL","LEGAL")
  },
};
